﻿using System;
using Castle.Core.Interceptor;

namespace FluentIoC.Configuration
{
    internal class MethodInterceptor : IInterceptor
    {
        public static IInvocation Invocation { get; private set; }

        public void Intercept(IInvocation invocation)
        {
            Invocation = invocation;

            if (invocation.Method.ReturnType != typeof(void))
            {
                try
                {
                    invocation.ReturnValue = Activator.CreateInstance(invocation.Method.ReturnType);
                }
                catch (MissingMethodException)
                {
                    invocation.ReturnValue = null;
                }
            }
        }
    }
}
